﻿#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
#endregion

//This namespace holds indicators in this folder and is required. Do not change it.
namespace HaiFeng
{
	/// <summary>
	/// The Forecast Oscillator (FOSC) is an extension of the linear regression based indicators 
	/// made popular by Tushar Chande. The Forecast Oscillator plots the percentage difference 
	/// between the forecast price (generated by an x-period linear regression line) and the actual 
	/// price. The oscillator is above zero when the forecast price is greater than the actual price.
	/// Conversely, it's less than zero if its below. In the rare case when the forecast price and the
	///  actual price are the same, the oscillator would plot zero. Actual prices that are persistently
	///  below the forecast price suggest lower prices ahead.  Likewise, actual prices that are persistently
	///  above the forecast price suggest higher prices ahead. Short-term traders should use shorter time
	///  periods and perhaps more relaxed standards for the required length of time above or below the
	///  forecast price. Long-term traders should use longer time periods and perhaps stricter standards
	///  for the required length of time above or below the forecast price. Chande also suggests plotting
	///  a three-day moving average trigger line of the Forecast Oscillator to generate early warnings of 
	/// changes in trend. When the oscillator crosses below the trigger line, lower prices are suggested. 
	/// When the oscillator crosses above the trigger line, higher prices are suggested.
	/// </summary>
	public class FOSC : Indicator
	{
		private TSF tsf;

		protected override void Init()
		{
			tsf = TSF(Input, 0, Period);
		}

		protected override void OnBarUpdate()
		{
			double input0 = Input[0];
			Value[0] = 100 * ((input0 - tsf[0]) / input0);
		}

		#region Properties
		[Range(1, int.MaxValue)]
		[Parameter("Period")]
		public int Period { get; set; }
		#endregion
	}

	#region generated code. Neither change nor remove.

	public partial class Indicator
	{
		private FOSC[] cacheFOSC;

		public FOSC FOSC(DataSeries input, int period)
		{
			if (cacheFOSC != null)
				for (int idx = 0; idx < cacheFOSC.Length; idx++)
					if (cacheFOSC[idx] != null && cacheFOSC[idx].Period == period && cacheFOSC[idx].EqualsInput(input))
						return cacheFOSC[idx];
			return CacheIndicator<FOSC>(new FOSC() { Period = period, Input = input }, ref cacheFOSC);
		}
	}

	public partial class Strategy
	{
		public FOSC FOSC(DataSeries input, int period)
		{
			return Indicator.FOSC(input, period);
		}
	}
}

#endregion
